﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class QLKhachHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }

            if (!IsPostBack)
            {
                getData();
            }
            else
            {
                if (Request.Params["btn_Tim"] != null)
                {
                    getData();
                }
                if (Request.Params["btn_Them"] != null)
                {
                    getData_Add();
                }
            }
        }

        private void getData()
        {

            IServerWeb sw = new ServerWeb();
            KhachHang_DAO dao = new KhachHang_DAO(sw.Connection);
            string hoTen = Request.Params["HoTen"];
            if (hoTen == null)
                hoTen = "";
            List<KhachHang> listData = dao.getListByKey(hoTen);


            Session.Add("page_KhachHang_loaiSP", listData);
            data_GridView.EditIndex = -1;
            Bind_Data();


            sw.closeConnection();

        }

        private void getData_Add()
        {

            IServerWeb sw = new ServerWeb();
            KhachHang_DAO dao = new KhachHang_DAO(sw.Connection);
            string hoTen = Request.Params["HoTen"];
            if (hoTen == null)
                hoTen = "";
            List<KhachHang> listData = dao.getListByKey(hoTen);
            listData.Add(new KhachHang()
            {
                DiaChi="",
                Email="",
                HoTen="",
                IDKhachHang = dao.createIDKhachHang(),
                IsStatus = 0,
                MatKhau = "",
                NgaySinh = Func.MinValue_SmallDatetime(),
                NgayTao = DateTime.Now,
                Phai = "",
                SoDienThoai = ""               
            });

            data_GridView.SelectedIndex = listData.Count - 1;
            data_GridView.EditIndex = listData.Count - 1;

            Session.Add("page_KhachHang_loaiSP", listData);
            Bind_Data();
            ((LinkButton)data_GridView.Rows[listData.Count - 1].Cells[7].Controls[0]).Text = "Thêm";

            sw.closeConnection();

        }

        private void Bind_Data()
        {
            data_GridView.DataSource = Session["page_KhachHang_loaiSP"];
            data_GridView.DataBind();
        }

        protected void data_GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            if (((LinkButton)data_GridView.Rows[e.RowIndex].Cells[7].Controls[0]).Text == "Thêm")
            {
                List<DTO.KhachHang> list = (List<DTO.KhachHang>)(Session["page_KhachHang_loaiSP"]);
                list.RemoveAt(e.RowIndex);
            }
            data_GridView.EditIndex = -1;
            Bind_Data();
        }

        protected void data_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = data_GridView.Rows[e.RowIndex];
            IServerWeb sw = new ServerWeb();
            KhachHang_DAO dao = new KhachHang_DAO(sw.Connection);
            List<KhachHang> list = (List<DTO.KhachHang>)(Session["page_KhachHang_loaiSP"]);

            dao.deleteByKey(list[row.DataItemIndex].IDKhachHang);

            list.RemoveAt(row.DataItemIndex);

            Bind_Data();
        }

        protected void data_GridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            data_GridView.EditIndex = e.NewEditIndex;
            Bind_Data();
        }

        protected void data_GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.RowIndex >= 0)
            {

                GridViewRow row = data_GridView.Rows[e.RowIndex];
                IServerWeb sw = new ServerWeb();
                KhachHang_DAO dao = new KhachHang_DAO(sw.Connection);
                List<KhachHang> list = (List<DTO.KhachHang>)(Session["page_KhachHang_loaiSP"]);
                //string maSP = list[row.DataItemIndex].MaSanPham;
                list[row.DataItemIndex].HoTen = Function.Func.beginConvertDataType(((TextBox)(row.Cells[1].Controls[0])).Text.Trim(), pDataType.String);
                list[row.DataItemIndex].Email = Function.Func.beginConvertDataType(((TextBox)(row.Cells[2].Controls[0])).Text.Trim(), pDataType.String);
                list[row.DataItemIndex].DiaChi = Function.Func.beginConvertDataType(((TextBox)(row.Cells[3].Controls[0])).Text.Trim(), pDataType.String);
                list[row.DataItemIndex].SoDienThoai = Function.Func.beginConvertDataType(((TextBox)(row.Cells[4].Controls[0])).Text.Trim(), pDataType.String);
                //list[row.DataItemIndex].Phai = Function.Func.beginConvertDataType(((TextBox)(row.Cells[5].Controls[0])).Text.Trim(), pDataType.String);                
                list[row.DataItemIndex].MatKhau = Function.Func.beginConvertDataType(((TextBox)(row.Cells[5].Controls[0])).Text.Trim(), pDataType.String);
                list[row.DataItemIndex].NgayTao = DateTime.Now;
                list[row.DataItemIndex].IsStatus = 0;
                //list[row.DataItemIndex].GiamGia = int.Parse(Function.Func.beginConvertDataType(((TextBox)(row.Cells[5].Controls[0])).Text, pDataType.Integer));

                //listPayment[row.DataItemIndex].updateBy(listPayment[row.DataItemIndex].PaymentID.ToString());
                if (((LinkButton)data_GridView.Rows[e.RowIndex].Cells[7].Controls[0]).Text == "Thêm")
                {
                    dao.add(list[row.DataItemIndex]);
                }
                else
                {
                    dao.updateByKey(list[row.DataItemIndex], list[row.DataItemIndex].IDKhachHang);
                }
                data_GridView.EditIndex = -1;
                //getData();
                Bind_Data();
            }
        }

    }
}